﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MyLibary.BIZ;

namespace MyLibary.DAL
{
    class DAL_HoaDonBan
    {
        private static DAL_Connection connect = new DAL_Connection();
        #region Metholds
        /// <summary>
        /// lấy danh sách hóa đơn bán
        /// </summary>
        /// <returns></returns>
        internal static List<BIZ_HoaDonBan> GetAll()
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM HoaDonBan");
            List<BIZ_HoaDonBan> result = new List<BIZ_HoaDonBan>();
            for (int i = 0; i < tb.Rows.Count; i++)
                result.Add(new BIZ_HoaDonBan(tb.Rows[i][0].ToString(), Convert.ToDateTime(tb.Rows[i][1].ToString()), tb.Rows[i][2].ToString(), Convert.ToInt32(tb.Rows[i][3].ToString()),Convert.ToDecimal(tb.Rows[i][4].ToString())));
            return result;
        }
        /// <summary>
        /// lấy danh sách hóa đơn bán có trạng thái bằng 0(--hóa đơn chưa giao hàng cho khách)
        /// </summary>
        /// <returns></returns>
        internal static List<BIZ_HoaDonBan> GetListStatusIsZero()
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM HoaDonBan WHERE TrangThai=0");
            List<BIZ_HoaDonBan> result = new List<BIZ_HoaDonBan>();
            for (int i = 0; i < tb.Rows.Count; i++)
                result.Add(new BIZ_HoaDonBan(tb.Rows[i][0].ToString(), Convert.ToDateTime(tb.Rows[i][1].ToString()), tb.Rows[i][2].ToString(), Convert.ToInt32(tb.Rows[i][3].ToString()),Convert.ToDecimal(tb.Rows[i][4].ToString())));
            return result;
        }
        /// <summary>
        /// lấy 1 hóa đơn bán theo mã hóa đơn bán
        /// </summary>
        /// <param name="MaHDBan"></param>
        /// <returns></returns>
        internal static BIZ_HoaDonBan GetHoaDonBan(string MaHDBan)
        {
            DataTable tb = connect.ExecuteSelect("SELECT * FROM HoaDonBan WHERE MaHDBan='"+MaHDBan+"'");
            return new BIZ_HoaDonBan(tb.Rows[0][0].ToString(), Convert.ToDateTime(tb.Rows[0][1].ToString()), tb.Rows[0][2].ToString(), Convert.ToInt32(tb.Rows[0][3].ToString()),Convert.ToDecimal(tb.Rows[0][4].ToString()));
        }
        /// <summary>
        /// thêm 1 record vào hóa đơn bán
        /// </summary>
        /// <param name="HDBan"></param>
        /// <returns></returns>
        internal static bool Insert(BIZ_HoaDonBan HDBan)
        {
            string formatDate = String.Format("{0:u}", HDBan.NgayLap);
            formatDate = formatDate.Substring(0, formatDate.Length - 1);
            string query=String.Format("INSERT INTO HoaDonBan VALUES('{0}','{1}','{2}','{3}','{4}')",HDBan.MaHDBan,formatDate,HDBan.MaKhachHang,HDBan.TrangThai,HDBan.TongTien);
            System.Console.WriteLine(query);
            return connect.ExecuteUpdate(query);
        }
        /// <summary>
        /// cập nhật 1 record Hóa đơn bán
        /// </summary>
        /// <param name="HDBan"></param>
        /// <returns></returns>
        internal static bool Update(BIZ_HoaDonBan HDBan)
        {
            string formatSQLDate = String.Format("{0:u}", HDBan.NgayLap);
            formatSQLDate = formatSQLDate.Substring(0, formatSQLDate.Length - 1);
            string query = String.Format("UPDATE HoaDonBan SET MaKhachHang='{0}',NgayLap='{1}',TrangThai='{2}',TongTien='{3}' WHERE MaHDBan='{4}'", HDBan.MaKhachHang, formatSQLDate, HDBan.TrangThai, HDBan.TongTien, HDBan.MaHDBan);
            return connect.ExecuteUpdate(query);
        }
        /// <summary>
        /// xóa 1 record trong Hóa Đơn Bán
        /// </summary>
        /// <param name="HDBan"></param>
        /// <returns></returns>
        internal static bool Delete(BIZ_HoaDonBan HDBan)
        {
            return connect.ExecuteUpdate("DELETE HoaDonBan WHERE MaHDBan='" + HDBan.MaHDBan + "'");
        }
        #endregion
    }
}
